package org.xhy.beanchameleon.core;

import org.xhy.beanchameleon.service.ChameleonManagerService;

import java.util.Collection;

/**
 * @description:
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-03-15 16:03
 */
public class ChameleonHandler<T> implements ChameleonExec<T> {

    private final ChameleonManagerService chameleonManager;

    public ChameleonHandler(ChameleonManagerService chameleonManager){
        this.chameleonManager = chameleonManager;
    }

    @Override
    public void exec(String name, T t) {
        final Collection<ChameleonBean> chameleons = chameleonManager.get(name);
        Do(chameleons,t);
    }

    private void Do(Collection<ChameleonBean> chameleons,T t){
        if (chameleons != null && chameleons.size() >0){
            for (Chameleon chameleon : chameleons) {
                chameleon.process(t);
            }
        }
    }
}
